package com.dk.mydemo.exception;

import java.util.ArrayList;
import java.util.List;
/** 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author dk 
* @date 2015年9月17日 下午7:01:39 
*  
*/
public class BusinessException extends RuntimeException {

	private static final long serialVersionUID = -5509543192074759614L;

	private static final String ArgKey = "#VAR#";

	/**
	 * 异常代码
	 */
	private String errorCode;
	/**
	 * 异常消息
	 */
	private String message;
	/**
	 * 异常消息参数
	 */
	private List<String> args = new ArrayList<String>();

	public BusinessException() {}

	public BusinessException(String errorCode) {
		this.errorCode = errorCode;
	}

	public BusinessException(String errorCode, String message) {
		this.errorCode = errorCode;
		this.message = message;
	}

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public String getMessage() {
		if (args == null || args.size() == 0) {
			return this.message;
		}
		String messageTemp = message;
		for (String argValue : args) {
			if(argValue == null) {
				messageTemp = messageTemp.replaceFirst(ArgKey, "null");
			} else {
				messageTemp = messageTemp.replaceFirst(ArgKey, argValue);
			}
		}
		return messageTemp;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void putArgument(String arg) {
		args.add(arg);
	}

	public String getArgument(int index) {
		if(index <= args.size() - 1 && index >= 0) {
			return args.get(index);
		} else {
			return null;
		}
	}

	public int getArgsCount() {
		return args.size();
	}
}
